//
//  YFBottomView.swift
//  微博
//
//  Created by 余仿 on 16/4/13.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

import SnapKit

class YFBottomView: UIView {

    //    控件懒加载
    private lazy var retweetBtn:UIButton = UIButton(title: "转发", imageName: "timeline_icon_retweet")

    private lazy var commentBtn:UIButton = UIButton(title: "评论", imageName: "timeline_icon_comment")

    private lazy var attitudeBtn:UIButton = UIButton(title: "赞", imageName: "timeline_icon_like")

    private lazy var sepLineLeft:UIView = UIView()

    private lazy var sepLineRight:UIView = UIView()

    //    添加到父视图,布局

    override init(frame: CGRect) {
        super.init(frame: frame)

        addSubview(retweetBtn)
        addSubview(commentBtn)
        addSubview(attitudeBtn)

        sepLineLeft.backgroundColor = UIColor.redColor()
        sepLineRight.backgroundColor = UIColor.redColor()

        addSubview(sepLineLeft)
        addSubview(sepLineRight)

        setupUI()
//        self.backgroundColor = UIColor.redColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI(){

        let btnWidth = screenWidth / 3
        retweetBtn.snp_makeConstraints { (make) -> Void in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(btnWidth)
        }

        commentBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetBtn.snp_right)
            make.top.bottom.equalTo(self)
            make.width.equalTo(retweetBtn)
        }

        attitudeBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.left.equalTo(commentBtn.snp_right)
            make.width.equalTo(retweetBtn)
        }

        let lineWidth = 1 / UIScreen.mainScreen().scale
        sepLineLeft.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self).offset(-8)
            make.top.equalTo(self).offset(8)
            make.left.equalTo(retweetBtn.snp_right)
            make.width.equalTo(lineWidth)
        }

        sepLineRight.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self).offset(-8)
            make.top.equalTo(self).offset(8)
            make.left.equalTo(commentBtn.snp_right)
            make.width.equalTo(lineWidth)
        }
    }
    //    绑定数据--ViewModel
    var statusViewModel:YFStatusViewModel?{

        didSet{

            retweetBtn.setTitle(statusViewModel?.retweetTitle, forState: .Normal)

            commentBtn.setTitle(statusViewModel?.commentTitle, forState: .Normal)

            attitudeBtn.setTitle(statusViewModel?.attitudeTitle, forState: .Normal)
        }

    }

}

extension UIButton{

    convenience init(title:String?,imageName:String,bgImgName:String = "timeline_card_middle_background",titleColor:UIColor = UIColor.darkTextColor()){

        self.init()

        setTitle(title, forState: .Normal)

        setTitleColor(titleColor, forState: .Normal)

        setImage(UIImage(named: imageName), forState: .Normal)

        //timeline_card_middle_background_highlighted
        setBackgroundImage(UIImage(named:bgImgName), forState: .Normal)
        setBackgroundImage(UIImage(named: bgImgName + "_highlighted"), forState: .Highlighted)

    }
}
